package pswvclass.pswvandroid2.com.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.PswvActivity;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/12/18.
 */
public class ForgetPassword extends AppCompatActivity implements View.OnClickListener {

    private String tag = "ForgetPassword";
    private TextView password_question;
    private TextView password_key;
    private Button sub;
    private int userId;
    private String passwordQuestion;
    protected static final int REGISTER_SUCCESS = 0;
    protected static final int ERROR = 1;
    protected static final int KEY_ERROR = 2;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Bundle data = getIntent().getBundleExtra("data");
        userId = data.getInt("userId");
        passwordQuestion = data.getString("passwordQuestion");
        setContentView(R.layout.pswv_password_retrieve);
        password_question = (TextView) findViewById(R.id.password_question);
        password_question.setText(passwordQuestion);
        password_key = (TextView) findViewById(R.id.password_key);
        sub = (Button) findViewById(R.id.sub);
        sub.setOnClickListener(this);
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case REGISTER_SUCCESS:
                    Intent intent = new Intent();
                    Bundle data = new Bundle();
                    data.putInt("userId", userId);
                    intent.putExtra("data", data);
                    intent.setClass(ForgetPassword.this, EditPassword.class);
                    startActivity(intent);
                    break;
                case KEY_ERROR:
                    Toast.makeText(ForgetPassword.this, "答案错误", Toast.LENGTH_SHORT).show();
                    break;
                case ERROR:
                    break;
                default:
                    break;
            }

        }

        ;

    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sub:
                final String password_key1 = password_key.getText().toString().trim();

                if (TextUtils.isEmpty(password_key1)) {
                    Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                new Thread() {
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/safeRetrieve";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("passwordKey", password_key1);
                            map1.put("id", userId + "");
                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                Message msg = Message.obtain();
                                msg.what = REGISTER_SUCCESS;
                                mHandler.sendMessage(msg);
                            } else if (result == 20) {
                                Message msg = Message.obtain();
                                msg.what = KEY_ERROR;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            mHandler.sendMessage(msg);
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }
}
